package com.hs.srm.admin.modules.notice.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.notice.dao.NoticeMainInfoDao;
import com.hs.srm.admin.modules.notice.dictionary.NoticeKind;
import com.hs.srm.admin.modules.notice.entity.NoticeMainInfoEntity;
import com.hs.srm.admin.modules.notice.service.NoticeMainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("noticeMainInfoService")
public class NoticeMainInfoServiceImpl extends ServiceImpl<NoticeMainInfoDao, NoticeMainInfoEntity> implements NoticeMainInfoService {

    @Autowired
    private NoticeMainInfoDao noticeMainInfoDao;

    /**
     * 通过通知类型获取通知用户id
     *
     * @param kind
     * @return
     */
    @Override
    public List<NoticeMainInfoEntity> getUserByKind(NoticeKind.KindEnum kind) {
        return noticeMainInfoDao.getUserByKind(kind);
    }

    /**
     * 保存通知
     *
     * @param userId
     * @param content
     * @return
     */
    @Override
    public int saveNotice(Long userId, String content) {
        NoticeMainInfoEntity noticeMainInfo = new NoticeMainInfoEntity();
        noticeMainInfo.setUserId(userId);
        noticeMainInfo.setCreateTime(new Date());
        noticeMainInfo.setContent(content);
        return noticeMainInfoDao.insert(noticeMainInfo);
    }

    /**
     * 批量保存
     *
     * @param kind    通知类型
     * @param content 通知内容
     */
    @Override
    public boolean saveNotice(NoticeKind.KindEnum kind, String content) {
        List<NoticeMainInfoEntity> NoticeMainInfoList = this.getUserByKind(kind);
        NoticeMainInfoList.forEach(item -> {
            item.setContent(content);
            item.setCreateTime(new Date());
        });
        return this.insertOrUpdateBatch(NoticeMainInfoList);
    }
}
